package com.sunway.netty.thirdexample;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class ChatClient {
	public static void main(String[] args) throws Exception {
		EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
		try {
			Bootstrap bootStrap = new Bootstrap();
			bootStrap.group(eventLoopGroup)
					.channel(NioSocketChannel.class)
					.handler(new ChatClientInitializer());
			
			//获取客户端和服务端的通讯管道
			Channel channel = bootStrap.connect("localhost", 8899).sync().channel();
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
			while(true) {
				channel.writeAndFlush(bufferedReader.readLine() + Constant.SOCKET_DELIMITER);
			}
		} finally {
			eventLoopGroup.shutdownGracefully();
		}
	}
}
